package dao

import (
	"errors"
	"fmt"

	"gitee.com/wao520/obtain_data/global"
	"gitee.com/wao520/obtain_data/model"
	"gorm.io/gorm"
)

// decrease stock
func DecreaseOneGoodsStock(goodsId int64, buyNum int) error {
	fmt.Println("DecreaseOneGoodsStock begin")
	//查询商品信息
	goodsOne := &model.Goods{}
	err := global.DBLink.Where("goodsId=?", goodsId).First(&goodsOne).Error
	//fmt.Println(goodsOne)
	if err != nil {
		return err
	}
	//得到库存
	stock := goodsOne.Stock
	fmt.Println(goodsOne.GoodsName, "当前库存:", stock)
	//fmt.Println(stock)
	if stock < buyNum || stock <= 0 {
		return errors.New("库存不足")
	}

	//减库存 .Debug()
	result := global.DBLink.Table("goods").Where("goodsId = ? ", goodsId).Update("stock", gorm.Expr("stock - ?", buyNum))
	if result.Error != nil {
		return result.Error
	} else {
		fmt.Println("成功减库存一次")
		return nil
	}
}
